import httpRequest from '@/commen/http_api/http_request.js'

//路由跳转
export const gourl = (url) => {
	uni.navigateTo({
		url: url,
		animationType: 'zoom-out',
		animationDuration: 2000
	})
};

//无图标提示框
export const showToast = (title) => {
	uni.showToast({
		title: title,
		icon: 'none'
	});
}

//成功提示框
export const showToastIcon = (title) => {
	uni.showToast({
		title: title,
		icon: 'success',
		mask: true,
	});
}

//图片点击事件
export const lookImage = (e) => {
	console.log(e);
	var src = e.cur; //获取data-src
	var imgList = e.urls;
	//图片预览
	uni.previewImage({
		current: src, // 当前显示图片的http链接
		urls: imgList // 需要预览的图片http链接列表
	})
}

// 数组去重方法
export const arraytoDup = (arr) => {
	let newArr = []
	for (let i = 0, l = arr.length; i < l; i++) {
		if (newArr.indexOf(arr[i]) === -1) {
			newArr.push(arr[i])
		}
	}
	return newArr;
};

/* 是否手机号码*/
export const isMobile = (value) => {
    const reg = /^[1][3,4,5,6,7,8,9][0-9]{9}$/;
    if (value == '' || value == undefined || value == null) {
      return false;
    } else {
      if ((!reg.test(value)) && value != '') {
        return false;
      }else {
        return true;
      }
    }
  };
  
  //检测是否登录
export const isLogin = () => { //这里因为要获取手机号才能有效登录，所已不能使用token判断用户是否登录
    let isLogin = uni.getStorageSync('isLogin');
    // let token = uni.getStorageSync('token');
    if (isLogin==''||isLogin==undefined) {
      uni.showModal({
        title: '提示',
        content: '您尚未登录，前去登录?',
        confirmText: '去登录',
        confirmColor: '#000',
        success(res) {
          if (res.confirm) {
			 uni.navigateTo({
			 	url:"/pageMember/login/login"
			 })
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
      return false;
    } else {
      return true;
    }
};
  
  //判断非空值
export const isDefine = (para) => {
    if (typeof para == "undefined" || para == "" || para == "[]" || para == null || para == undefined) {
      return false;
    } else if (typeof para == "number" || para == "number" || para == "null") {
      return true;
    } else if (para == "undefined") {
      return true;
    } else {
      for (var o in para) {
        return true;
      }
      return false;
    }
  };

// 上传图片调用方法
// this.$uploadPic(3, function(res){
//   console.log(res);
// });
//选择上传方式  maxNum: 可上传最大数量，callback：回调方法
export const uploadPic = (maxNum, callback) => {
	uni.showActionSheet({
		itemList: ['从相册中选', '拍摄'],
		success(res) {
			if (res.tapIndex == 0) {
				upload('album', maxNum, callback);
			} else if (res.tapIndex == 1) {
				upload('camera', maxNum, callback);
			}
		},
	})
}
// 上传图片
function upload(kind, maxNum, callback) {
	var imgArr = [];
	let _this = this;
	uni.chooseImage({
		count: maxNum,
		sizeType: ['original', 'compressed'],
		sourceType: [kind],
		success(res) {
			var tempFilePaths = res.tempFilePaths;
			console.log(tempFilePaths)
			for (var i = 0; i < tempFilePaths.length; i++) {
				uni.uploadFile({
					url: httpRequest.uploadUrl, // 仅为示例，非真实的接口地址
					filePath: tempFilePaths[i],
					name: 'file',
					success(ret) {
						uni.hideLoading();
						var data = JSON.parse(ret.data)
						console.log(data);
						if (data.code == 1) {
							imgArr.push(data.data);
						} else {
							uni.showToast({
								title: '上传失败，请重新上传',
								icon: 'error',
								mask: true,
							})
							return;
						}
						if (imgArr.length == tempFilePaths.length) {
							callback(imgArr);
						}
					},
					fail(res) {
						uni.hideLoading();
						uni.showToast({
							title: '上传失败，请重新上传',
							icon: 'error',
							mask: true,
						})
					}
				});
			}
		}
	})
}
